package com.xinchuang.mapper.targetSystem;

import com.xinchuang.entity.targetSystem.system.TargetRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName TargetRelationMapper
 * @Description: 目标关联表
 * @auther xukl
 * @data 2021/4/6  9:53
 * @Version V1.0
 */
public interface TargetRelationMapper {

    @Select(value = "SELECT target_name FROM target_base_data WHERE target_id = #{targetId}")
    String selectTargetNameByTargetId(@Param("targetId") String targetId);

    @Select(value = {
            "SELECT id,target_id,relation_id,relationskip,source,create_time FROM target_relation WHERE target_id = #{targetId}"
    })
    @Results(id = "targetResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "relationId", column = "relation_id"),
            @Result(property = "relationName", column = "relation_id", one = @One(select = "com.zkwc.targetmanage.dao.TargetRelationMapper.selectTargetNameByTargetId")),
            @Result(property = "relationskip", column = "relationskip"),
            @Result(property = "source", column = "source"),
            @Result(property = "createTime", column = "create_time")
    })
    List<TargetRelation> findByTargetId(String targetId);

    @Insert(value = {
            "<script>",
            "INSERT INTO target_relation ",
            "(id,target_id,relation_id,relationskip,source) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.targetId},#{item.relationId},#{item.relationskip},#{item.source})",
            "</foreach>",
            "</script>"
    })
    int save(List<TargetRelation> listRelation);

    @Delete(value = "DELETE FROM target_relation WHERE target_id = #{targetId}")
    int deleteByTargetId(String targetId);

    @Delete(value = "DELETE FROM target_relation WHERE id = #{id}")
    int deleteTargetRelation(@Param("id") String relationId);

}


